using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace SurveyHost.Core.DAL
{
	/// <summary>
	/// Strongly-typed collection for the Stem class.
	/// </summary>
	[Serializable]
	public partial class StemCollection : ActiveList<Stem, StemCollection> 
	{	   
		public StemCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Stem table.
	/// </summary>
	[Serializable]
	public partial class Stem : ActiveRecord<Stem>
	{
		#region .ctors and Default Settings
		
		public Stem()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Stem(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Stem(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Stem(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Stem", TableType.Table, DataService.GetInstance("SurveyHost"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarStemId = new TableSchema.TableColumn(schema);
				colvarStemId.ColumnName = "StemId";
				colvarStemId.DataType = DbType.Guid;
				colvarStemId.MaxLength = 0;
				colvarStemId.AutoIncrement = false;
				colvarStemId.IsNullable = false;
				colvarStemId.IsPrimaryKey = true;
				colvarStemId.IsForeignKey = false;
				colvarStemId.IsReadOnly = false;
				
						colvarStemId.DefaultSetting = @"(newid())";
				colvarStemId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarStemId);
				
				TableSchema.TableColumn colvarMeasureName = new TableSchema.TableColumn(schema);
				colvarMeasureName.ColumnName = "MeasureName";
				colvarMeasureName.DataType = DbType.String;
				colvarMeasureName.MaxLength = 100;
				colvarMeasureName.AutoIncrement = false;
				colvarMeasureName.IsNullable = false;
				colvarMeasureName.IsPrimaryKey = false;
				colvarMeasureName.IsForeignKey = true;
				colvarMeasureName.IsReadOnly = false;
				colvarMeasureName.DefaultSetting = @"";
				
					colvarMeasureName.ForeignKeyTableName = "Measure";
				schema.Columns.Add(colvarMeasureName);
				
				TableSchema.TableColumn colvarQuestionText = new TableSchema.TableColumn(schema);
				colvarQuestionText.ColumnName = "QuestionText";
				colvarQuestionText.DataType = DbType.String;
				colvarQuestionText.MaxLength = 2000;
				colvarQuestionText.AutoIncrement = false;
				colvarQuestionText.IsNullable = false;
				colvarQuestionText.IsPrimaryKey = false;
				colvarQuestionText.IsForeignKey = false;
				colvarQuestionText.IsReadOnly = false;
				colvarQuestionText.DefaultSetting = @"";
				colvarQuestionText.ForeignKeyTableName = "";
				schema.Columns.Add(colvarQuestionText);
				
				TableSchema.TableColumn colvarRank = new TableSchema.TableColumn(schema);
				colvarRank.ColumnName = "Rank";
				colvarRank.DataType = DbType.Int32;
				colvarRank.MaxLength = 0;
				colvarRank.AutoIncrement = false;
				colvarRank.IsNullable = false;
				colvarRank.IsPrimaryKey = false;
				colvarRank.IsForeignKey = false;
				colvarRank.IsReadOnly = false;
				
						colvarRank.DefaultSetting = @"((0))";
				colvarRank.ForeignKeyTableName = "";
				schema.Columns.Add(colvarRank);
				
				TableSchema.TableColumn colvarAnswerTypeName = new TableSchema.TableColumn(schema);
				colvarAnswerTypeName.ColumnName = "AnswerTypeName";
				colvarAnswerTypeName.DataType = DbType.String;
				colvarAnswerTypeName.MaxLength = 100;
				colvarAnswerTypeName.AutoIncrement = false;
				colvarAnswerTypeName.IsNullable = true;
				colvarAnswerTypeName.IsPrimaryKey = false;
				colvarAnswerTypeName.IsForeignKey = true;
				colvarAnswerTypeName.IsReadOnly = false;
				colvarAnswerTypeName.DefaultSetting = @"";
				
					colvarAnswerTypeName.ForeignKeyTableName = "AnswerObjectType";
				schema.Columns.Add(colvarAnswerTypeName);
				
				TableSchema.TableColumn colvarAnswerTypeConfiguration = new TableSchema.TableColumn(schema);
				colvarAnswerTypeConfiguration.ColumnName = "AnswerTypeConfiguration";
				colvarAnswerTypeConfiguration.DataType = DbType.String;
				colvarAnswerTypeConfiguration.MaxLength = 1000;
				colvarAnswerTypeConfiguration.AutoIncrement = false;
				colvarAnswerTypeConfiguration.IsNullable = true;
				colvarAnswerTypeConfiguration.IsPrimaryKey = false;
				colvarAnswerTypeConfiguration.IsForeignKey = false;
				colvarAnswerTypeConfiguration.IsReadOnly = false;
				colvarAnswerTypeConfiguration.DefaultSetting = @"";
				colvarAnswerTypeConfiguration.ForeignKeyTableName = "";
				schema.Columns.Add(colvarAnswerTypeConfiguration);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["SurveyHost"].AddSchema("Stem",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("StemId")]
		public Guid StemId 
		{
			get { return GetColumnValue<Guid>("StemId"); }

			set { SetColumnValue("StemId", value); }

		}

		  
		[XmlAttribute("MeasureName")]
		public string MeasureName 
		{
			get { return GetColumnValue<string>("MeasureName"); }

			set { SetColumnValue("MeasureName", value); }

		}

		  
		[XmlAttribute("QuestionText")]
		public string QuestionText 
		{
			get { return GetColumnValue<string>("QuestionText"); }

			set { SetColumnValue("QuestionText", value); }

		}

		  
		[XmlAttribute("Rank")]
		public int Rank 
		{
			get { return GetColumnValue<int>("Rank"); }

			set { SetColumnValue("Rank", value); }

		}

		  
		[XmlAttribute("AnswerTypeName")]
		public string AnswerTypeName 
		{
			get { return GetColumnValue<string>("AnswerTypeName"); }

			set { SetColumnValue("AnswerTypeName", value); }

		}

		  
		[XmlAttribute("AnswerTypeConfiguration")]
		public string AnswerTypeConfiguration 
		{
			get { return GetColumnValue<string>("AnswerTypeConfiguration"); }

			set { SetColumnValue("AnswerTypeConfiguration", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
				
		private SurveyHost.Core.DAL.AnswerCollection colAnswerRecords;
		public SurveyHost.Core.DAL.AnswerCollection AnswerRecords
		{
			get
			{
				if(colAnswerRecords == null)
					colAnswerRecords = new SurveyHost.Core.DAL.AnswerCollection().Where(Answer.Columns.StemId, StemId).Load();
				return colAnswerRecords;			
			}

			set { colAnswerRecords = value; }

		}

		
		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a AnswerObjectType ActiveRecord object related to this Stem
		/// 
		/// </summary>
		public SurveyHost.Core.DAL.AnswerObjectType AnswerObjectType
		{
			get { return SurveyHost.Core.DAL.AnswerObjectType.FetchByID(this.AnswerTypeName); }

			set { SetColumnValue("AnswerTypeName", value.AnswerTypeName); }

		}

		
		
		/// <summary>
		/// Returns a Measure ActiveRecord object related to this Stem
		/// 
		/// </summary>
		public SurveyHost.Core.DAL.Measure Measure
		{
			get { return SurveyHost.Core.DAL.Measure.FetchByID(this.MeasureName); }

			set { SetColumnValue("MeasureName", value.MeasureName); }

		}

		
		
		#endregion
		
		
		
		#region Many To Many Helpers
		
		 
		public SurveyHost.Core.DAL.SurveyCollection GetSurveyCollection() { return Stem.GetSurveyCollection(this.StemId); }

		public static SurveyHost.Core.DAL.SurveyCollection GetSurveyCollection(Guid varStemId)
		{
			SubSonic.QueryCommand cmd = new SubSonic.QueryCommand(
				"SELECT * FROM Survey INNER JOIN Answer ON "+
				"Survey.SurveyId=Answer.SurveyId WHERE Answer.StemId=@StemId", Stem.Schema.Provider.Name);
			
			cmd.AddParameter("@StemId", varStemId, DbType.Guid);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			SurveyCollection coll = new SurveyCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}

		
		public static void SaveSurveyMap(Guid varStemId, SurveyCollection items)
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			QueryCommand cmdDel = new QueryCommand("DELETE FROM Answer WHERE StemId=@StemId", Stem.Schema.Provider.Name);
			cmdDel.AddParameter("@StemId", varStemId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (Survey item in items)
			{
				Answer varAnswer = new Answer();
				varAnswer.SetColumnValue("StemId", varStemId);
				varAnswer.SetColumnValue("SurveyId", item.GetPrimaryKeyValue());
				varAnswer.Save();
			}

		}

		public static void SaveSurveyMap(Guid varStemId, System.Web.UI.WebControls.ListItemCollection itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM Answer WHERE StemId=@StemId", Stem.Schema.Provider.Name);
			cmdDel.AddParameter("@StemId", varStemId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (System.Web.UI.WebControls.ListItem l in itemList) 
			{
				if (l.Selected) 
				{
					Answer varAnswer = new Answer();
					varAnswer.SetColumnValue("StemId", varStemId);
					varAnswer.SetColumnValue("SurveyId", l.Value);
					varAnswer.Save();
				}

			}

		}

		public static void SaveSurveyMap(Guid varStemId , Guid[] itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM Answer WHERE StemId=@StemId", Stem.Schema.Provider.Name);
			cmdDel.AddParameter("@StemId", varStemId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (Guid item in itemList) 
			{
				Answer varAnswer = new Answer();
				varAnswer.SetColumnValue("StemId", varStemId);
				varAnswer.SetColumnValue("SurveyId", item);
				varAnswer.Save();
			}

		}

		
		public static void DeleteSurveyMap(Guid varStemId) 
		{
			QueryCommand cmdDel = new QueryCommand("DELETE FROM Answer WHERE StemId=@StemId", Stem.Schema.Provider.Name);
			cmdDel.AddParameter("@StemId", varStemId);
			DataService.ExecuteQuery(cmdDel);
		}

		
		#endregion
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varStemId,string varMeasureName,string varQuestionText,int varRank,string varAnswerTypeName,string varAnswerTypeConfiguration)
		{
			Stem item = new Stem();
			
			item.StemId = varStemId;
			
			item.MeasureName = varMeasureName;
			
			item.QuestionText = varQuestionText;
			
			item.Rank = varRank;
			
			item.AnswerTypeName = varAnswerTypeName;
			
			item.AnswerTypeConfiguration = varAnswerTypeConfiguration;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varStemId,string varMeasureName,string varQuestionText,int varRank,string varAnswerTypeName,string varAnswerTypeConfiguration)
		{
			Stem item = new Stem();
			
				item.StemId = varStemId;
				
				item.MeasureName = varMeasureName;
				
				item.QuestionText = varQuestionText;
				
				item.Rank = varRank;
				
				item.AnswerTypeName = varAnswerTypeName;
				
				item.AnswerTypeConfiguration = varAnswerTypeConfiguration;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string StemId = @"StemId";
			 public static string MeasureName = @"MeasureName";
			 public static string QuestionText = @"QuestionText";
			 public static string Rank = @"Rank";
			 public static string AnswerTypeName = @"AnswerTypeName";
			 public static string AnswerTypeConfiguration = @"AnswerTypeConfiguration";
						
		}

		#endregion
	}

}

